import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import {
  getGoodListTool,
  geRandListTool,
  getShangChengTool,
  getModelInfoTool,
  getContactTool,
  getCreateProductTool,
  delProductTool,
  getCategoryTool,
  getSaleDateToole,
} from "./tools/index";
import { z } from "zod";
// 创建 server instance
const server = new McpServer({
  name: "weather",
  version: "1.0.0",
  capabilities: {
    resources: {},
    tools: {},
  },
});
// Helper function 用于发送 NWS API 请求
server.tool("get-good-list", "获取到产品列表", {}, async () => {
  let infoList: any = await getGoodListTool();
  return infoList;
});
// Helper function 用于发送 NWS API 请求
server.tool(
  "get-rand-list",
  "获取销售列表",
  {
    token: z.string().describe("用户token"),
    num: z.string().describe("前多少名销售排行"),
  },
  async ({ num, token }) => {
    let data = {
      num,
      token,
    };
    let infoList: any = await geRandListTool(data);
    return infoList;
  }
);
// Helper function 用于发送 NWS API 请求
server.tool("get-kefu-info", "获取模型信息", {}, async () => {
  let infoList: any = await getModelInfoTool();
  return infoList;
});
server.tool("get-shangcheng-info", "获取主营信息", {}, async () => {
  let infoList: any = await getShangChengTool();
  return infoList;
});
server.tool("get-contact-info", "获取联系方式", {}, async () => {
  let infoList: any = await getContactTool();
  return infoList;
});
server.tool(
  "create-product-tool",
  "创建商品",
  {
    discount: z
      .string()
      .describe("折扣：无折扣为1,9折为0.9,8折为0.8，依次类推"),
    name: z.string().describe("商品名称"),
    description: z.string().describe("商品描述"),
    categoryId: z.string().describe("商品分类id"),
    imgs: z.string().describe("商品封面"),
    categoryList: z.array(
      z
        .object({
          value: z.string().describe("规格名称"),
          price: z.string().describe("该商品规格对应的价格"),
          store: z.string().describe("商品规格对应的库存"),
        })
        .describe("产品规格对应的信息数组")
    ),
  },
  async ({ discount, name, description, categoryId, imgs, categoryList }) => {
    let categoryArr: any = await getCategoryTool();
    let _categoryId = "";
    if (isNaN(Number(categoryId))) {
      let _data = categoryArr.filter((item: any) => {
        return item.name.includes(categoryId);
      });
      if (_data.length > 0) {
        _categoryId = _data[0].id;
      }
    } else {
      _categoryId = categoryId;
    }

    let data = {
      discount,
      name,
      description,
      categoryId: _categoryId,
      imgs,
      categoryList,
    };
    let infoList: any = await getCreateProductTool(data);
    return infoList;
  }
);
server.tool(
  "del-product-tool",
  "通过is删除商品",
  {
    id: z.string().describe("需要删除的产品id"),
  },
  async ({ id }) => {
    let infoList: any = await delProductTool(id);
    return infoList;
  }
);
// getSaleDateToole
server.tool(
  "get-sale-date-tool",
  "获取到销售情况",
  {
    token: z.string().describe("权限token"),
  },
  async ({ token }) => {
    let infoList: any = await getSaleDateToole(token);
    return infoList;
  }
);

async function main() {
  const transport = new StdioServerTransport();
  await server.connect(transport);
  console.error("Weather MCP Server running on stdio");
}

main().catch((error) => {
  console.error("Fatal error in main():", error);
  process.exit(1);
});
